package com.zlkj.pro.modular.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.admin.model.dto.VersionAdminDTO;
import com.zlkj.pro.modular.admin.model.vo.VersionAdminVO;
import com.zlkj.pro.modular.admin.service.VersionAdminService;
import com.zlkj.pro.modular.common.entity.VersionEntity;
import com.zlkj.pro.modular.common.mapper.VersionMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author 孙铭泽
 * @date 2022/9/24
 * @time 12:36
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class VersionAdminServiceImpl extends ServiceImpl<VersionMapper, VersionEntity> implements VersionAdminService {

    @Override
    public PageResult<VersionAdminVO> findPage(VersionAdminDTO versionAdminDTO) {
        Integer pageNo = versionAdminDTO.getPageNo();
        Integer pageSize = versionAdminDTO.getPageSize();
        Integer code = versionAdminDTO.getCode();
        String name = versionAdminDTO.getName();
        String urlType = versionAdminDTO.getUrlType();
        LambdaQueryWrapper<VersionEntity> versionEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        versionEntityLambdaQueryWrapper.in(Objects.nonNull(code), VersionEntity::getCode, code);
        versionEntityLambdaQueryWrapper.like(StrUtil.isNotBlank(name), VersionEntity::getName, name);
        versionEntityLambdaQueryWrapper.like(StrUtil.isNotBlank(urlType), VersionEntity::getUrlType, urlType);
        versionEntityLambdaQueryWrapper.orderByDesc(VersionEntity::getCreateTime);
        Page<VersionEntity> versionEntityPage = super.page(new Page<>(pageNo, pageSize), versionEntityLambdaQueryWrapper);
        long total = versionEntityPage.getTotal();
        List<VersionEntity> records = versionEntityPage.getRecords();
        List<VersionAdminVO> versionAdminList = ConvertUtil.turn(records, VersionAdminVO.class);
        return PageResultFactory.createPageResult(versionAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<VersionAdminVO> findList(VersionAdminDTO versionAdminDTO) {
        LambdaQueryWrapper<VersionEntity> versionEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        List<VersionEntity> versionEntityList = super.list(versionEntityLambdaQueryWrapper);
        return ConvertUtil.turn(versionEntityList, VersionAdminVO.class);
    }

    @Override
    public VersionAdminVO findDetail(VersionAdminDTO versionAdminDTO) {
        Long id = versionAdminDTO.getId();
        LambdaQueryWrapper<VersionEntity> versionEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        versionEntityLambdaQueryWrapper.eq(VersionEntity::getId, id);
        VersionEntity versionEntity = super.getOne(versionEntityLambdaQueryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(versionEntity, VersionAdminVO.class);
    }

    @Override
    public boolean add(VersionAdminDTO versionAdminDTO) {
        VersionEntity versionEntity = ConvertUtil.turn(versionAdminDTO, VersionEntity.class);
        return super.save(versionEntity);
    }

    @Override
    public boolean edit(VersionAdminDTO versionAdminDTO) {
        VersionEntity versionEntity = ConvertUtil.turn(versionAdminDTO, VersionEntity.class);
        return super.updateById(versionEntity);
    }

    @Override
    public boolean delete(VersionAdminDTO versionAdminDTO) {
        return super.removeById(versionAdminDTO);
    }

}
